package com.atlas.alpha;

import java.util.*;

import android.content.*;
import android.database.*;
import android.database.sqlite.*;

public class WorkoutDatabaseAdapter {
	private Workout wrkOut;
	static final String DATABASE_NAME = "wrkOutDB.db";
	static final int DATABASE_VERSION = 1;
	static final String DATABASE_CREATE = "create table " + "WORKOUT" + "( " + "ID"
			+ " integer primary key autoincrement,"
			+ "ZONEID text,WORKOUTNAME  text,REPS text,SETS text,TIME text,DESCRIPTION text); ";
	public SQLiteDatabase db;
	private final Context context;
	private DataBaseHelper dbhelper;

	public WorkoutDatabaseAdapter(Context newContext) {
		context = newContext;
		dbhelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	public WorkoutDatabaseAdapter open() throws SQLException {
		db = dbhelper.getWritableDatabase();
		return this;
	}

	public void close() {
		db.close();
	}

	public SQLiteDatabase getDBInstance() {
		return db;
	}

	/**
	 * addWorkout - Add a workout to the workout database
	 * 
	 * @param newWorkout
	 *            - Workout object - The workout to be added
	 */
	public void addWorkout(Workout newWorkout) {
		ContentValues newValues = new ContentValues();

		newValues.put("ZONEID", newWorkout.getZoneID());
		newValues.put("WORKOUTNAME", newWorkout.getWorkoutName());
		newValues.put("REPS", newWorkout.getReps());
		newValues.put("SETS", newWorkout.getSets());
		newValues.put("TIME", newWorkout.getTime());
		newValues.put("DESCRIPTION", newWorkout.getDesc());

		wrkOut = newWorkout;

		db.insert("WORKOUT", null, newValues);
	}

	/**
	 * removeWorkout - Remove a workout from the workout database
	 * 
	 * @param woName
	 *            - Name of the workout to remove
	 * @return
	 */
	public int removeWorkout(String woName) {
		String what = "WORKOUTNAME=?";
		return db.delete("WORKOUT", what, new String[] { woName });
	}

	/**
	 * getWorkoutByName - Find a workout by the workout name
	 * 
	 * @param wName
	 *            - Name of the workout to find
	 * @return - A workoutout object containing the workout data
	 */
	public Workout getWorkoutByName(String wName) {

		Cursor cursor = db.query("WORKOUT", null, " WORKOUTNAME=?", new String[] { wName }, null, null, null);
		// If the workout does not exist
		if (cursor.getCount() < 1) {
			cursor.close();
			return null;
		}
		// Return the cursor to the beginning
		cursor.moveToFirst();
		// Get the workout
		wrkOut.setZoneID(cursor.getString(cursor.getColumnIndex("ZONEID")));
		wrkOut.setWorkoutName(wName);
		wrkOut.setReps(cursor.getString(cursor.getColumnIndex("REPS")));
		wrkOut.setSets(cursor.getString(cursor.getColumnIndex("SETS")));
		wrkOut.setTime(cursor.getString(cursor.getColumnIndex("TIME")));
		wrkOut.setDesc(cursor.getString(cursor.getColumnIndex("DESCRIPTION")));
		cursor.close();
		return wrkOut;
	}

	/**
	 * 
	 * @param id
	 *            - Zone ID of the workouts being searched
	 * @return -
	 */
	public Workout getWorkoutByID(String id) {
		Cursor cursor = db.query("WORKOUT", null, " ZONEID=?", new String[] { id }, null, null, null);
		// if the workout does not exist
		if (cursor.getCount() < 1) {
			cursor.close();
			return null;
		}
		// move cursor to first row
		cursor.moveToFirst();
		// Get the workout
		wrkOut.setZoneID(id);
		wrkOut.setWorkoutName(cursor.getString(cursor.getColumnIndex("WORKOUTNAME")));
		;
		wrkOut.setReps(cursor.getString(cursor.getColumnIndex("REPS")));
		wrkOut.setSets(cursor.getString(cursor.getColumnIndex("SETS")));
		wrkOut.setTime(cursor.getString(cursor.getColumnIndex("TIME")));
		wrkOut.setDesc(cursor.getString(cursor.getColumnIndex("DESCRIPTION")));
		cursor.close();
		return wrkOut;
	}

	public List<Workout> getWorkoutList(String id) {
		List<Workout> wrkOutList = new ArrayList<Workout>();
		Cursor cursor = db.query("WORKOUT", null, " ZONEID=?", new String[] { id }, null, null, null);
		if (cursor.moveToFirst()) {
			do {
				Workout wkOut = new Workout();
				if (id == cursor.getString(cursor.getColumnIndex("ZONEID"))) {
					wkOut.setZoneID(id);
					wkOut.setWorkoutName(cursor.getString(cursor.getColumnIndex("WORKOUTNAME")));
					wkOut.setReps(cursor.getString(cursor.getColumnIndex("REPS")));
					wkOut.setSets(cursor.getString(cursor.getColumnIndex("SETS")));
					wkOut.setTime(cursor.getString(cursor.getColumnIndex("TIME")));
					wkOut.setDesc(cursor.getString(cursor.getColumnIndex("DESCRIPTION")));
					wrkOutList.add(wkOut);
				}
			} while (cursor.moveToNext());
		}
		return wrkOutList;
	}

	/**
	 * Get the last workout
	 * 
	 * @return
	 */
	public Workout getLastWorkout() {
		return wrkOut;
	}
}
